﻿CREATE PROCEDURE [dbo].[proc_OrderF_Create]
	(
		@OId uniqueidentifier
		,@CusName	nvarchar(50)
		,@CusPerson	nvarchar(50)
		,@CusPersonTel	nvarchar(50)
		,@CusDescription	nvarchar(1000)
		,@CusEstimate	nvarchar(200)
		,@TypeId	int
		,@TypeName	nvarchar(50)
		,@Salesman	nvarchar(50)
		,@SalesmanAccount	nvarchar(50)
		,@TradeDate	datetime
		,@CreateDate	datetime
		,@YingShouPrice	decimal(18, 2)
		,@ShiShouPrice	decimal(18, 2)
		,@Discount	nvarchar(50)
		,@PaymentTypeId	int
		,@PaymentType	nvarchar(50)
		,@BankId	int
		,@Bank	nvarchar(50)
		,@PaymentDescription	nvarchar(200)
		,@OtherDescription	nvarchar(1000)
		,@DepId	int
		,@CompanyId	int
		,@StateId int
	)
AS
	Begin
		Select @Salesman=EName,@DepId=DepartmentId,@CompanyId=CompanyId From Employee Where UserName=@SalesmanAccount
		Insert Into OrderF
		(
			OId
			,CusName
			,CusPerson
			,CusPersonTel
			,CusDescription
			,CusEstimate
			,TypeId
			,TypeName
			,Salesman
			,SalesmanAccount
			,TradeDate
			,CreateDate
			,YingShouPrice
			,ShiShouPrice
			,Discount
			,PaymentTypeId
			,PaymentType
			,BankId
			,Bank
			,PaymentDescription
			,OtherDescription
			,DepId
			,CompanyId
			,StateId
			,IsGreen
		)
		Values
		(
			@OId
			,@CusName
			,@CusPerson
			,@CusPersonTel
			,@CusDescription
			,@CusEstimate
			,@TypeId
			,@TypeName
			,@Salesman
			,@SalesmanAccount
			,@TradeDate
			,@CreateDate
			,@YingShouPrice
			,@ShiShouPrice
			,@Discount
			,@PaymentTypeId
			,@PaymentType
			,@BankId
			,@Bank
			,@PaymentDescription
			,@OtherDescription
			,@DepId
			,@CompanyId
			,@StateId
			,0
		)
		Insert Into Order_PaymentRecord
		(
			OId
			,CusName
			,SalesmanAccount
			,Salesman
			,CreateDate
			,Prices
			,PaymentTypeId
			,PaymentType
			,BankId
			,Bank
			,SWId
			,SWName
			,FinanceFlag
			,CompanyId
		)
		Values
		(
			@OId
			,@CusName
			,@SalesmanAccount
			,@Salesman
			,@CreateDate
			,@ShiShouPrice
			,@PaymentTypeId
			,@PaymentType
			,@BankId
			,@Bank
			,1
			,'首款'
			,0
			,@CompanyId
		)
		Update Order_Item Set OId=@OId,Occupancy=NULL,TradeDate=@TradeDate Where Occupancy=@SalesmanAccount
	End
	RETURN
